package com.jm.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.jm.dao.LoginDao;
import com.jm.dao.UserDao;
import com.jm.dao.Utils;
import com.jm.viewmodel.User;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.net.httpserver.Authenticator.Success;

import sun.util.resources.CalendarData;

public class UserAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6377472443458828206L;

	private Map<String, Object> qUser;
	private Map<String, Object> addUserMap;
	private Map<String, Object> agentsMap;
	private Map<String, Object> authoritUserMap;
	private Map<String, Object> addAreaMap;

	private File identifyicon; // 上传的文件
	private String imageFileName; // 文件名称
	private String imageContentType; // 文件类型

	public Map<String, Object> getAddAreaMap() {
		return addAreaMap;
	}

	public void setAddAreaMap(Map<String, Object> addAreaMap) {
		this.addAreaMap = addAreaMap;
	}

	public Map<String, Object> getAuthoritUserMap() {
		return authoritUserMap;
	}

	public void setAuthoritUserMap(Map<String, Object> authoritUserMap) {
		this.authoritUserMap = authoritUserMap;
	}

	public Map<String, Object> getAgentsMap() {
		return agentsMap;
	}

	public void setAgentsMap(Map<String, Object> agentsMap) {
		this.agentsMap = agentsMap;
	}

	public Map<String, Object> getAddUserMap() {
		return addUserMap;
	}

	public void setAddUserMap(Map<String, Object> addUserMap) {
		this.addUserMap = addUserMap;
	}

	public File getIdentifyicon() {
		return identifyicon;
	}

	public void setIdentifyicon(File identifyicon) {
		this.identifyicon = identifyicon;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}

	public Map<String, Object> getqUser() {
		return qUser;
	}

	public void setqUser(Map<String, Object> qUser) {
		this.qUser = qUser;
	}

	@SuppressWarnings("deprecation")
	public String addUser() {
		addUserMap = new HashMap<String, Object>();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			String idnumber = "";
			String upuid = request.getParameter("upuid");
			String name = request.getParameter("name");
			String wechat = request.getParameter("wechat");
			String phone = request.getParameter("phone");
			idnumber = request.getParameter("idnumber");
			String level = request.getParameter("level");
			String areaid = request.getParameter("areaid");
			// String email = request.getParameter("email");
			// String detailAdress = request.getParameter("detailadress");

			// 以下为图片上传部分
			// 保存到根目录下的uploadImages文件夹下
			String realPath = ServletActionContext.getServletContext().getRealPath("upload/identify");
			String path = request.getContextPath();
			String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
			String picPath = "";// ;
			UserDao mDao = new UserDao();
			int result = mDao.addUser(level, name, phone, idnumber, wechat, picPath, Long.valueOf(upuid), "",
					Integer.valueOf(areaid));
			if (result == 1) {
				addUserMap.put("result", true);
			} else if (result == 2) {
				addUserMap.put("result", false);
				addUserMap.put("msg", "用户已经注册");
			} else if (result == 3) {
				addUserMap.put("result", false);
				addUserMap.put("msg", "一个区域只能发展一个代理，请联系上级重新生成链接");
			} else {
				addUserMap.put("result", false);
			}

			/*
			 * if(identifyicon !=null){ String picName =
			 * String.valueOf(System.currentTimeMillis()+idnumber+".jpg");
			 * picPath = "upload/identify/"+picName; File savefile = new
			 * File(new File(realPath),picName); try {
			 * FileUtils.copyFile(identifyicon, savefile); //插入用户信息 UserDao mDao
			 * = new UserDao(); mDao.addUser(level,name, phone, idnumber,
			 * wechat,picPath,Long.valueOf(upuid), ""); addUserMap.put("result",
			 * true); } catch (IOException | NumberFormatException |
			 * NoSuchAlgorithmException e) { // TODO Auto-generated catch block
			 * e.printStackTrace(); addUserMap.put("result", false); } }else{
			 * //图片获取失败 addUserMap.put("result", false); }
			 */
			this.setAddUserMap(addUserMap);
		} catch (Exception e) {
			// TODO: handle exception
			addUserMap.put("result", false);
		}

		return SUCCESS;
	}

	public String getUserById() {
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String uid = String.valueOf(request.getParameter("uid"));
		System.out.println("request uid .." + uid);
		
		if (Utils.isNumeric(uid)) {

			LoginDao mDao = new LoginDao();
			try {
				User sUser = mDao.getUser(Long.parseLong(uid.trim()));
				qUser = new HashMap<>();
				if (sUser == null) {
					qUser.put("result", sUser);
					this.setqUser(qUser);
				} else {
					qUser.put("result", sUser);
					this.setqUser(qUser);
				}
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("getUserById failed ");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}
	
	public  boolean isNumeric(String str){
		  for (int i = 0; i < str.length(); i++){
		   System.out.println(str.charAt(i));
		   if (!Character.isDigit(str.charAt(i))){
		    return false;
		   }
		  }
		  return true;
	}

	public String getAgents() {
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String uid = String.valueOf(request.getParameter("uid"));
		String isauthorize = String.valueOf(request.getParameter("isauthorize"));

		agentsMap = new HashMap<>();
		UserDao mDao = new UserDao();
		@SuppressWarnings("unchecked")
		List<User> agents = mDao.getAgents(Long.valueOf(uid), Integer.valueOf(isauthorize));

		if (agents.size() == 0) {
			agentsMap.put("msg", "没有代理申请");
		} else {
			for (User u : agents) {
				agentsMap.put(String.valueOf(agents.indexOf(u)), u);
			}
		}
		this.setAgentsMap(agentsMap);
		return SUCCESS;
	}

	public String authoritUser() {
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String isauthorize = request.getParameter("isauthorize");
		String uid = request.getParameter("uid");
		UserDao mDao = new UserDao();
		boolean isUpdate = mDao.authoritUser(Integer.valueOf(isauthorize), Long.valueOf(uid));
		authoritUserMap = new HashMap<>();
		if (isUpdate) {
			authoritUserMap.put("result", true);
			authoritUserMap.put("msg", "授权成功");
		} else {
			authoritUserMap.put("result", false);
			authoritUserMap.put("msg", "授权失败");
		}
		this.setAuthoritUserMap(authoritUserMap);
		return SUCCESS;
	}

}
